import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    beforeEnter: (to, from, next) => {
      if (to.path == '/user') {
        next()
      } else if (!(localStorage.getItem('User'))) {
        alert('您还没有登陆，请先登陆！');
        next('/user')
      } else {
        // alert('加入购物车')
        next('/')
      }
      next();
    }
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue')
  }
]

// router.beforeEach((to, from, next) => {
//   if (to.matched.some(r => r.meta.requireAuth)) {
//     if (store.state.token) {
//       next();
//     }
//     else {
//       next({
//         name: 'login',
//         param: {redirect: to.fullPath}
//       })
//     }
//   }
//   else {
//     next();
//   }
// })

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
